package cmcc.dbi.execUtil;

import lombok.extern.slf4j.Slf4j;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

@Slf4j
public class AESEncryptionUtil {
    //    private final static String sKey = "XXXXXXXXXXXXXXXXXXXXXXX";
    private static final String sKey = "lrZeFGsK57TPwoDv";        //密码需要是16/24/32位
    private static final String ALGORITHM = "AES";
    private static final String TRANSFORMATION = "AES";

    //生成一个AES算法的key
    public static void setSkey(){
        //AES 中的 KEY 必须是 16 字节（128位）长 的整数倍
        int STRING_LENGTH = 16;
        //所有字符，用于生成随机字符
        String CHAR_SET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        // 创建SecureRandom实例
        SecureRandom secureRandom = new SecureRandom();

        // 生成16字符的随机字符串
        StringBuilder sb = new StringBuilder(STRING_LENGTH);
        for (int i = 0; i < STRING_LENGTH; i++) {
            int randomIndex = secureRandom.nextInt(CHAR_SET.length());
            sb.append(CHAR_SET.charAt(randomIndex));
        }
        //sKey = sb.toString();
        //System.out.println("当前sKey："+sKey);
    }

    public static String encrypt(String content) {
        if (content == null) {
            throw new IllegalArgumentException("加密内容不能为空");
        }
        try {
            byte[] key = sKey.getBytes(StandardCharsets.UTF_8);
            //SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
            SecretKeySpec secretKeySpec = new SecretKeySpec(key, ALGORITHM);
            //加密
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            byte[] encryptedData = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
            return java.util.Base64.getEncoder().encodeToString(encryptedData);
        } catch (Exception e) {
            log.info("加密失败:" + e.getMessage());
            return content;
        }
    }

    public static String decrypt(String content) {
        try {
            byte[] key = sKey.getBytes(StandardCharsets.UTF_8);
            /* SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
            //先用base64解密
            byte[] encrypted1 = new Base64().decode(content);
            //解密为字符串
            String decryptStr = aes.decryptStr(encrypted1, StandardCharsets.UTF_8);
            return decryptStr;*/

            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            SecretKeySpec secretKeySpec = new SecretKeySpec(key, ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            byte[] decodedData = java.util.Base64.getDecoder().decode(content);
            byte[] decryptedData = cipher.doFinal(decodedData);
            return new String(decryptedData, StandardCharsets.UTF_8);
        } catch (Exception e) {
            log.info("解密失败:" + e.getMessage());
            return content;
        }
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        //setSkey();
        String pwdStr = "dH1#c8C3&6Fd%3X99EKJH";
        System.out.println("原始：" + pwdStr);
        String encrypted = encrypt(pwdStr);
        System.out.println("加密后：" + encrypted);
        String decrypted = decrypt(encrypted);
        System.out.println("解密后：" + decrypted);

    }
}
